import { mixin_approval_edit } from "@tapui/mixins/mixin_approval_edit"
import { putAction, postAction, deleteAction, getAction } from '@tapui/utils/http'
export const mixin_approval_edit_edit = {
  mixins: [mixin_approval_edit],
  data() {
    var checkRate = (rule, value, callback) => {
      if (!value) {
        callback()
        return true
      }
      value = value + ""
      if (value.indexOf(".") !== -1 && value.split('.').length > 2) {
        callback(new Error('请输入正确格式的金额')) // 防止输入多个小数点
      } else if (value.indexOf(".") !== -1 && value.split('.')[1].length > 2) {
        callback(new Error('只保留两位小数')) // 小数点后两位
      } else {
        if (value > 100) {
          callback(new Error('不能超过100'))
        } else {
          callback()
        }
      }
    }
    return {
      amountRateValidate: { validator: checkRate, trigger: 'blur' },
      validateNumberRequire: [
        // 必填
        { required: 'true', message: '请输入内容', trigger: 'change' },
        { validator: checkRate, trigger: 'blur' }
      ]
    }
  },
  methods: {
    async addBase() {
      if (this.beforeSubmit()) {
        this.saveLoading = true
        if (this.isAdd) {
          return await postAction(this.url.add, this.model)
            .then(async res => {
              if (res.success) {
                return await this.queryData(res.result.id).then(_ => {
                  this.isAdd = false
                  this.afterSubmit()
                  this.saveLoading = false
                  return true
                })
              } else {
                this.$tap.alert(res.message, "warning")
                this.afterSubmit()
                this.saveLoading = false
                return false
              }
            })
            .catch(err => {
              this.saveLoading = false
              return false
            })
        } else {
          return await putAction(this.url.edit, this.model)
            .then(async res => {
              if (res.success) {
                return await this.queryData(this.model.id).then(async _ => {
                  this.isAdd = false
                  this.afterSubmit()
                  this.saveLoading = false
                  return true
                })
              } else {
                this.$tap.alert(res.message, "warning")
                this.afterSubmit()
                this.saveLoading = false
                return false
              }
            })
            .catch(err => {
              this.saveLoading = false
              return false
            })
        }
      }
    },
    doAdd() {
      this.saveLoading = true
      // 增加唯一单号
      this.$set(this.model, 'billuid', this.uid)
      postAction(this.url.add, this.model)
        .then((res) => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            this.queryData(res.result.id).then((_) => {
              this.afterSubmit()
            })
            this.isAdd = false
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.saveLoading = false
        })
        .catch((err) => {
          this.saveLoading = false
        })
    }
  }
}
